package cn.itcast.controller;

import cn.itcast.pojo.User;
import cn.itcast.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: taft
 * @Date: 2018-9-25 11:29
 */
@RestController
@RequestMapping("consumer")
public class UserController {

   @Autowired
    UserService userService;

    @Autowired
    private DiscoveryClient discoveryClient;// Eureka客户端，可以获取到服务实例信息

    @GetMapping("user")
    public ResponseEntity<List<User>> queryUserById(@RequestParam("ids") List<Long> ids){

        List<User> users = new ArrayList<>();


        //根据服务名称获取服务对象
//        List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
//
//
//        //获取具体的服务对象
//        ServiceInstance instance = instances.get(0);//i++,0,1,2,3,4,0
//
//        //获取服务的主机名和端口号
//        String host = instance.getHost();
//        Integer port = instance.getPort();



        for (Long id : ids) {

            User user = userService.queryUserById(id);
            users.add(user);

        }


        return ResponseEntity.ok(users);
    }
}
